package com.tianxiao.faas.biz.service.corner;

import com.tianxiao.faas.api.param.corner.OrderHistoryDetailsResult;
import com.tianxiao.faas.api.param.corner.ShopDetailsResult;
import com.tianxiao.faas.common.constants.CommonConstants;
import com.tianxiao.faas.common.exception.biz.BizException;
import com.tianxiao.faas.mapper.dao.OrderHistoryModelMapper;
import com.tianxiao.faas.mapper.dao.ShopModelMapper;
import com.tianxiao.faas.mapper.model.OrderHistoryModel;
import com.tianxiao.faas.mapper.model.ShopModel;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class ShareService {

    @Resource
    private OrderHistoryModelMapper orderHistoryModelMapper;

    @Resource
    private ShopModelMapper shopModelMapper;

    public Object get(String shareId) {
        Object object = null;
        OrderHistoryModel orderHistoryModel = orderHistoryModelMapper.selectByShareId(shareId);
        if (orderHistoryModel == null) {
            throw new BizException("分享记录不存在");
        }
        ShopModel shopModel = shopModelMapper.selectByPrimaryKey(orderHistoryModel.getShopId());
        if (shopModel == null) {
            throw new BizException("分享餐厅不存在");
        }
        Integer type = orderHistoryModel.getType();
        if (CommonConstants.HISTORY_ORDER.equals(type)) {
            OrderHistoryDetailsResult orderHistoryDetailsResult = new OrderHistoryDetailsResult();
            orderHistoryDetailsResult.setId(orderHistoryModel.getId());
            orderHistoryDetailsResult.setShopName(shopModel.getName());
            orderHistoryDetailsResult.setShopImgs(shopModel.getImages());
            orderHistoryDetailsResult.setLogo(shopModel.getLogo());
            orderHistoryDetailsResult.setResult(orderHistoryModel.getResult());
            orderHistoryDetailsResult.setShareId(orderHistoryModel.getShareId());
            orderHistoryDetailsResult.setUserRequirement(orderHistoryModel.getUserRequirement());
            orderHistoryDetailsResult.setShopId(shopModel.getId());
            object = orderHistoryDetailsResult;
        }
        if (CommonConstants.HISTORY_REPORT.equals(type)) {
            ShopDetailsResult shopDetailsResult = new ShopDetailsResult();
            shopDetailsResult.setId(orderHistoryModel.getId());
            shopDetailsResult.setImages(shopModel.getImages());
            shopDetailsResult.setShareId(orderHistoryModel.getShareId());
            shopDetailsResult.setShopReport(orderHistoryModel.getResult());
            shopDetailsResult.setAddress(shopModel.getAddress());
            shopDetailsResult.setName(shopModel.getName());
            shopDetailsResult.setOpenTime(shopModel.getOpenTime());
            shopDetailsResult.setPerCapitaConsumption(shopModel.getPerCapitaConsumption());
            object = shopDetailsResult;
        }
        return object;
    }
}
